<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Adds new attribute</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="domelement.removeattributens.html">DOMElement::removeAttributeNS</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="domelement.setattributenode.html">DOMElement::setAttributeNode</a></div>
 <div class="up"><a href="class.domelement.html">DOMElement</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="domelement.setattribute" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DOMElement::setAttribute</h1>
  <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">DOMElement::setAttribute</span> &mdash; <span class="dc-title">Adds new attribute</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-domelement.setattribute-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type"><a href="class.domattr.html" class="type DOMAttr">DOMAttr</a></span> <span class="methodname"><strong>DOMElement::setAttribute</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$value</code></span>
   )</div>

  <p class="para rdfs-comment">
   Sets an attribute with name <em><code class="parameter">name</code></em> to the given
   value. If the attribute does not exist, it will be created.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-domelement.setattribute-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">name</code></em></dt>

     <dd>

      <p class="para">
       The name of the attribute.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">value</code></em></dt>

     <dd>

      <p class="para">
       The value of the attribute.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-domelement.setattribute-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   The new <a href="class.domattr.html" class="classname">DOMAttr</a> or <strong><code>FALSE</code></strong> if an error occurred.
  </p>
 </div>

 <div class="refsect1 errors" id="refsect1-domelement.setattribute-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   <dl>

    
     <dt>
<strong><code>DOM_NO_MODIFICATION_ALLOWED_ERR</code></strong></dt>

     <dd>

      <p class="para">
       Raised if the node is readonly.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-domelement.setattribute-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5303">
    <p><strong>Example #1 Setting an attribute</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$doc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">(</span><span style="color: #DD0000">"1.0"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$node&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createElement</span><span style="color: #007700">(</span><span style="color: #DD0000">"para"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$newnode&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">appendChild</span><span style="color: #007700">(</span><span style="color: #0000BB">$node</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$newnode</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setAttribute</span><span style="color: #007700">(</span><span style="color: #DD0000">"align"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"left"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-domelement.setattribute-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="domelement.hasattribute.html" class="methodname" rel="rdfs-seeAlso">DOMElement::hasAttribute()</a> - Checks to see if attribute exists</span></li>
    <li class="member"><span class="methodname"><a href="domelement.getattribute.html" class="methodname" rel="rdfs-seeAlso">DOMElement::getAttribute()</a> - Returns value of attribute</span></li>
    <li class="member"><span class="methodname"><a href="domelement.removeattribute.html" class="methodname" rel="rdfs-seeAlso">DOMElement::removeAttribute()</a> - Removes attribute</span></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="domelement.removeattributens.html">DOMElement::removeAttributeNS</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="domelement.setattributenode.html">DOMElement::setAttributeNode</a></div>
 <div class="up"><a href="class.domelement.html">DOMElement</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
